package com.rxt.thermopro.ui.dialog

import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.mikephil.charting.components.LimitLine
import com.rxt.ble.data.BleDevice
import com.rxt.thermopro.R
import com.rxt.thermopro.adapter.ProfileItemAdapter
import com.rxt.thermopro.bean.ProfileItem
import com.rxt.thermopro.ui.fragment.BluetoothListDialog

class BottomDialog : DialogFragment() {

    private lateinit var contentView: View
    private var isShow = false
    private var mScreenDensity = 0f
    private var mScreenHeight = 0
    private var mScreenWidth = 0
    private lateinit var adapter: ProfileItemAdapter
    private lateinit var recyclerView: RecyclerView
    private val profileItemList = mutableListOf<ProfileItem>()
    private var mListener: OnDialogProfileListener? = null
    private val mHandler = Handler(Looper.getMainLooper())
    private var currentProfileIndex = -1


    interface OnDialogProfileListener {
        fun onItemClick(currentProfileIndex: Int, itemPosition: Int)
    }

    fun setOnDialogBluetoothListener(listener: OnDialogProfileListener) {
        mListener = listener
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireActivity())

        contentView = View.inflate(requireContext(), R.layout.dialog_bottom, null)
        initListener()

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(contentView)
        dialog.setCanceledOnTouchOutside(false)

        val displayMetrics = DisplayMetrics()
        requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
        mScreenDensity = displayMetrics.density
        mScreenHeight = displayMetrics.heightPixels
        mScreenWidth = displayMetrics.widthPixels

        isCancelable = true
        val window: Window? = dialog.window
        if (window != null) {
            window.setBackgroundDrawable(ColorDrawable(resources.getColor(android.R.color.transparent)))
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            val lp: WindowManager.LayoutParams = window.attributes
            lp.gravity = Gravity.BOTTOM
            lp.width = mScreenWidth
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT
            window.attributes = lp
        }
        return dialog
    }

    private fun initListener() {
        adapter = ProfileItemAdapter(profileItemList)
        recyclerView = contentView.findViewById(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = adapter

        adapter.setOnItemClick(object : ProfileItemAdapter.OnItemClickListener {
            override fun onItemClick(view: View, position: Int) {
                mHandler.post {
                    println("123-> onItemChecked=$position")
                    adapter.checkPosition(position)
                    mListener?.onItemClick(
                        currentProfileIndex,
                        position
                    )
                }
                hideDialog()
            }
        })
    }

    fun showData(list: List<ProfileItem>, fragmentManager: FragmentManager) {
        adapter.addAll(list)
        showDialog(fragmentManager)
    }

    fun updateData(list: List<ProfileItem>, index: Int) {
        if (profileItemList.size > 0) {
            profileItemList.clear()
        }
        profileItemList.addAll(list)
        currentProfileIndex = index
    }

    fun showDialog(fragmentManager: FragmentManager) {
        try {
            if (!isShow) {
                this.show(fragmentManager, "TAG")
                isShow = true
            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("123->", "showDialog Exception $e")
        }
    }

    fun hideDialog() {
        if (isShow) {
            dismiss()
            isShow = false
        }
    }


    override fun onResume() {
        super.onResume()
        isShow = true
    }

    override fun onPause() {
        super.onPause()
        isShow = false
    }

    override fun show(manager: FragmentManager, tag: String?) {
        val ft = manager.beginTransaction()
        ft.add(this, tag)
        ft.commitAllowingStateLoss()
    }


}